#include "pch.h"
#include <iostream>

using namespace std;

long long gcd(long long a, long long b)
{
	return b == 0 ? a : gcd(b, a%b);
}

class Rational
{
public:
	Rational(int = 1, int = 3);
	friend ostream& operator<< (ostream&, Rational);
	Rational operator+ (int);
	Rational operator+ (Rational&);
	friend Rational operator+(int, Rational&);
	Rational operator- ();
	Rational operator++ (int);
	operator double();
	Rational reduction(Rational);

private:
	int up;
	int down;
};
Rational::Rational(int i, int j)
{
	up = i;
	down = j;
}
Rational Rational::reduction(Rational result)
{
	if (result.down < 0)
	{
		result.up = -result.up;
		result.down = -result.down;
	}
	if (result.up == 0)
		result.down = 1;
	else
	{
		int d = gcd(abs(result.up), abs(result.down));
		result.up /= d;
		result.down /= d;
	}
	return result;
}
Rational Rational::operator+ (int i)
{
	Rational result;
	result.up = up + down * i;
	return reduction(result);
}
Rational Rational::operator+ (Rational& f)
{
	Rational result;
	result.up = up * f.down + f.up*down;
	result.down = down * f.down;
	return reduction(result);
}
Rational operator+(int i, Rational& r)
{
	Rational result;
	result.up = r.up + i * r.down;
	result.down = r.down;
	result = result.reduction(result);
	return result;
}
Rational Rational::operator- ()
{
	return Rational(-up, down);
}
Rational Rational::operator++(int)
{
	up = up + down;
	return *this;
}
Rational::operator double()
{
	return (double)up / down;
}
ostream& operator<< (ostream& out, Rational r)
{
	r = r.reduction(r);
	if (r.down == 0)
		throw r.down;
	else if (r.down == 1)
		out << r.up << endl;
	else if (abs(r.up) > r.down)
		out << r.up / r.down << " " << abs(r.up) % r.down << "/" << r.down << endl;
	else
		out << r.up << "/" << r.down << endl;
	return out;
}

int main()
{
	Rational rat0;
	cout << rat0 << endl;
	Rational rat1 = -rat0;
	cout << rat1 << endl;
	Rational rat2 = rat1 + 34;
	cout << rat2 << endl;
	Rational rat3;
	rat3 = 12 + rat2;
	rat3++;
	cout << rat3 << endl;
	cout << (double)rat3 << endl;
	double sum = 0;
	for (int i = 1;i < 50;i++)
	{
		Rational a(i, i + 1);
		sum = sum + (double)a;
	}
	cout << endl << sum << endl;
	return 0;
}